<?php

namespace App\Nova;

use Illuminate\Validation\Rules;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\Gravatar;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Password;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;

class User extends Resource
{
    public static string $model = \App\Models\User::class;

    public static $title = 'account';

    public static $search = [
        'account', 'email',
    ];

    public static $group = '账号';

    public static function label(): string
    {
        return '会员';
    }

    public function fields(NovaRequest $request)
    {
        return [
            ID::make()->sortable(),

            Gravatar::make()->maxWidth(50),

            Text::make('账号', 'account')
                ->sortable()
                ->rules('required', 'max:255'),

            Text::make('邮箱', 'email')
                ->sortable()
                ->rules('required', 'email', 'max:254')
                ->creationRules('unique:users,email')
                ->updateRules('unique:users,email,{{resourceId}}'),

            Password::make('密码', 'password')
                ->onlyOnForms()
                ->creationRules('required', Rules\Password::defaults())
                ->updateRules('nullable', Rules\Password::defaults()),

            Boolean::make('激活', 'active')->sortable()->default(true),

            DateTime::make('生成时间', 'created_at')->onlyOnDetail(),
            DateTime::make('更新时间', 'updated_at')->exceptOnForms(),
        ];
    }
}
